package com.inray.nbs.utils;



public class PageUtil {
	public static Page getPage(int pageSize, int currentPage, Integer totalCount) {
		pageSize = getPageSize(pageSize);

		currentPage = getCurrentPage(currentPage);

		totalCount = getTotalCount(totalCount);
		
		int totalPage = getTotalPage(pageSize, totalCount);

		int beginIndex = getBeginIndex(pageSize, currentPage);

		int endIndex = getEndIndex(pageSize, beginIndex, totalCount);

		boolean hasNextPage = isHasNextPage(currentPage);

		boolean hasPrvPage = isHasPrvPage(currentPage, totalPage);

		return new Page(pageSize, currentPage, totalCount, beginIndex,
				endIndex, totalPage, hasNextPage, hasPrvPage);
	}

	public static int getPageSize(int pageSize) {
		return pageSize;
	}

	public static int getCurrentPage(int currentPage) {
		return currentPage;
	}

	public static int getTotalCount(int totalCount) {
		return totalCount;
	}

	public static int getBeginIndex(int pageSize, int currentPage) {

		return (currentPage - 1) * pageSize + 1;
	}

	public static int getEndIndex(int pageSize, int beginIndex, int totalCount) {
		if ((beginIndex + pageSize - 1) > totalCount
				|| (beginIndex + pageSize - 1) == totalCount) {

			return totalCount;

		} else {
			return beginIndex + pageSize - 1;
		}
	}

	public static int getTotalPage(int pageSize, int totalCount) {
		return totalCount % pageSize == 0 ? totalCount / pageSize : totalCount
				/ pageSize + 1;
	}

	public static boolean isHasNextPage(int currentPage) {
		return currentPage > 1;
	}

	public static boolean isHasPrvPage(int currentPage, int totalPage) {
		return currentPage < totalPage;
	}

	public static int  getCurrentPageByCheck(String currentPage){
		
		if (!MathUitl.mathStringCheck(currentPage)) {
			return 1;
		}else{
			return Integer.parseInt(currentPage);
		}
	}
	
	
	public static int  getTotalCountByCheck(String totalCount){
		
		if (!MathUitl.mathStringCheck(totalCount)) {
			return 0;
		}else{
			return Integer.parseInt(totalCount);
		}
	}
	
	

}
